<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回当前 Unix 时间戳和微秒数</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.localtime.html">« localtime</a></li>
      <li style="float: right;"><a href="function.mktime.html">mktime »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.datetime.html">Date/Time 函数</a></li>
    <li>返回当前 Unix 时间戳和微秒数</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.microtime" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">microtime</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">microtime</span> &mdash; <span class="dc-title">返回当前 Unix 时间戳和微秒数</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.microtime-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>microtime</strong></span>(<span class="methodparam"><span class="type">bool</span> <code class="parameter">$as_float</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type"><span class="type">string</span>|<span class="type">float</span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>microtime()</strong></span> 返回当前 Unix
   时间戳以及微秒数。本函数仅在支持 gettimeofday()
   系统调用的操作系统下可用。
  </p>
  <p class="para">
   对于性能测量，建议使用 <span class="function"><a href="function.hrtime.html" class="function">hrtime()</a></span>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.microtime-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">as_float</code></dt>

     <dd>

      <p class="para">
       如果使用并设置为 <strong><code>true</code></strong>，<span class="function"><strong>microtime()</strong></span>
       将返回浮点数而不是字符串，具体查看下面的返回值相关描述。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.microtime-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   默认情况下，<span class="function"><strong>microtime()</strong></span> 返回“msec sec”形式的 <span class="type">string</span>，其中
   <code class="literal">sec</code> 是自 Unix 纪元（格林威治标准时间 1970 年 1 月 1 日
   0:00:00）以来的秒数，<code class="literal">msec</code> 是自 <code class="literal">sec</code>
   以来经过的微秒，表示为秒数的小数部分。
  </p>
  <p class="para">
   如果 <code class="parameter">as_float</code> 设置为 <strong><code>true</code></strong>，然后 <span class="function"><strong>microtime()</strong></span>
   返回 <span class="type">float</span>，表示自 Unix 纪元以来的当前时间，以秒为单位，精确到最接近的微秒。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.microtime-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2101">
    <p><strong>示例 #1 计时脚本执行时间</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$time_start&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Sleep&nbsp;一会<br /></span><span style="color: #0000BB">usleep</span><span style="color: #007700">(</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$time_end&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$time_end&nbsp;</span><span style="color: #007700">-&nbsp;</span><span style="color: #0000BB">$time_start</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Did&nbsp;nothing&nbsp;in&nbsp;</span><span style="color: #0000BB">$time</span><span style="color: #DD0000">&nbsp;seconds\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
   <div class="example" id="example-2102">
    <p><strong>示例 #2 <span class="function"><strong>microtime()</strong></span> 和 <code class="literal">REQUEST_TIME_FLOAT</code></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;随机&nbsp;sleep&nbsp;时间<br /></span><span style="color: #0000BB">usleep</span><span style="color: #007700">(</span><span style="color: #0000BB">mt_rand</span><span style="color: #007700">(</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10000</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;在&nbsp;$_SERVER&nbsp;超全局数组中&nbsp;REQUEST_TIME_FLOAT&nbsp;是有效的。<br />//&nbsp;包含请求开始的时间戳，精度为微秒。<br /></span><span style="color: #0000BB">$time&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">microtime</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">)&nbsp;-&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">"REQUEST_TIME_FLOAT"</span><span style="color: #007700">];<br /><br />echo&nbsp;</span><span style="color: #DD0000">"Did&nbsp;nothing&nbsp;in&nbsp;</span><span style="color: #0000BB">$time</span><span style="color: #DD0000">&nbsp;seconds\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.microtime-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.time.html" class="function" rel="rdfs-seeAlso">time()</a> - 返回当前的 Unix 时间戳</span></li>
    <li class="member"><span class="function"><a href="function.hrtime.html" class="function" rel="rdfs-seeAlso">hrtime()</a> - 获取系统的高精度时间</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>